package com.chenyitun.cloudadmin.common.controller;

import com.chenyitun.cloudcommon.utils.BeanValidators;
import com.chenyitun.cloudcommon.utils.ExceptionUtils;
import com.chenyitun.cloudadmin.constants.Constants;
import com.chenyitun.cloudadmin.domain.User;
import com.chenyitun.cloudadmin.utils.ShiroUtils;
import com.google.common.collect.Maps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.ConstraintViolationException;
import javax.validation.Validator;
import java.util.Map;

/**
 * @author chenyitun@163.com
 * @date 2019-03-15
 */
@Controller
public class BaseController {
	/**
	 * 日志对象
	 */
	protected Logger 	LOGGER = LoggerFactory.getLogger(getClass());
	/**
	 * 验证Bean实例对象
	 */
	@Autowired
	protected Validator validator;

	public User getUser() {
		User user = ShiroUtils.getUser();
		return user;
	}

	public Long getUserId() {
		Long userId = getUser().getUserId();
		return userId;
	}

	public String getUsername() {
		return getUser().getUsername();
	}



	/**
	 * 服务端参数有效性验证
	 * @param object 验证的实体对象
	 * @param groups 验证组
	 * @return 验证成功：返回true；严重失败：将错误信息添加到 message 中
	 */
	protected boolean beanValidator(Model model, Object object, Class<?>... groups) {
		try{
			BeanValidators.validateWithException(validator, object, groups);
		}catch(ConstraintViolationException ex){
			LOGGER.error(ExceptionUtils.errInfo(ex));
			return false;
		}
		return true;
	}

	/**
	 * 服务端参数有效性验证
	 * @param object 验证的实体对象
	 * @param groups 验证组
	 * @return 验证成功：返回true；严重失败：将错误信息添加到 flash message 中
	 */
	protected boolean beanValidator(RedirectAttributes redirectAttributes, Object object, Class<?>... groups) {
		try{
			BeanValidators.validateWithException(validator , object , groups);
		}catch(ConstraintViolationException ex){
			LOGGER.error(ExceptionUtils.errInfo(ex));
			return false;
		}
		return true;
	}

	/**
	 * 服务端参数有效性验证
	 * @param object 验证的实体对象
	 * @param groups 验证组，不传入此参数时，同@Valid注解验证
	 * @return 验证成功：继续执行；验证失败：抛出异常跳转400页面。
	 */
	protected boolean  beanValidator(Object object, Class<?>... groups) {

		try {
			BeanValidators.validateWithException(validator, object, groups);
		} catch (ConstraintViolationException e) {
			LOGGER.error(ExceptionUtils.errInfo(e));
			return false;
		}
		return true ;
	}

	protected Model isAdmin(Model model){
		User user = ShiroUtils.getUser();
		if(user != null && !Constants.ISADMIN.equals(user.getUserId().toString())){
		    model.addAttribute("deptId" , user.getDeptId());
        }
		model.addAttribute("userType" , ShiroUtils.getUser().getUserType());
		return model;
	}


	protected boolean isAdmin(){
		User user = ShiroUtils.getUser();
		if(user != null && Constants.ISADMIN.equals(user.getUserId().toString())){
		    return true;
        }
		return false;
	}

	protected Map<String , Object> mapModel(){
		Map<String , Object> params = Maps.newHashMap();
		return params;
	}
	protected Map<String , Object> mapModel(String key , Object value){
		Map<String , Object> params = mapModel();
		params.put(key , value);
		return params;
	}
	protected Map<String , Object> mapModel(Map<String , Object> map){
		Map<String , Object> params = mapModel();
		params.putAll(map);
		return params;
	}

	protected Map<String , Object> rebuildListParams(Map<String , Object> params){
		User user = ShiroUtils.getUser() ;
		String userType = user.getUserType();
		if(userType.equals("200001")){
			params.remove("dataScope");
			params.put("agentId" , user.getUserId());
		}
		return params;
	}

}